package org.ucas.faker.web.utils.report;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;


import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.ucas.faker.web.utils.model.Coordinator;

public class ReportWord {
	public static BaseFont chinese;
	public static BaseFont kaiti;
	public ReportWord() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) throws DocumentException, MalformedURLException, IOException, InterruptedException {
		String FILE_DIR = "C:/Users/PIN/Desktop/patent/null/word/";
		Document doc = new Document();  
		//doc.topMargin();
		
		doc.setMargins(50, 50, 350,0);  //left//right//up//down
		RtfWriter2 writer = RtfWriter2.getInstance(doc, new FileOutputStream(FILE_DIR + "createSamplePDF.doc"));  
		//PdfWriter writer = RtfWriter2.getInstance(doc, new FileOutputStream(FILE_DIR + "createSamplePDF.pdf")); 
		//writer.setStrictImageSequence(true);
		doc.open();
		insertTitle(doc,"专利分析报告");
		doc.setMargins(50, 50, 200,0);
		String des = "专利报告是国家知识产权局根据专利权人或者利害关系人的请求，对相关实用新型专利或者外观设计专利进行检索，并就该专利是否符合专利法及其实施细则规定的授权条件进行分析和评价，作出的报告。专利报告是人民法院或者管理专利工作的部门审理、处理专利侵权纠纷的证据.";
		//File uploadFile = new File(realPathDirectory.getPath() + "/" + fileName);
		Image img = Image.getInstance("C:/Users/PIN/Desktop/patent/null/PatentImg/1.png");
		insertPair(doc,des,img);
		insertPair(doc,des,img);
		doc.close();
		writer.close();
		
	}
	public static void makeReportWord(Long taskId) throws DocumentException, IOException {
		String FILE_DIR_WORD = Coordinator.fileBasePath+taskId+Coordinator.pdfPath;
		Document doc = new Document();  
		//doc.topMargin();
		
		File realPathDirectory = new File(FILE_DIR_WORD);
	    if (realPathDirectory == null || !realPathDirectory.exists()) {
	      realPathDirectory.mkdirs();
	    }
	    System.out.println(FILE_DIR_WORD);
		doc.setMargins(50, 50, 350,0);  //left//right//up//down
		RtfWriter2 writer = RtfWriter2.getInstance(doc, new FileOutputStream(FILE_DIR_WORD + "/result.docx")); 
		//File uploadFile = new File(realPathDirectory.getPath() + "/" + "result.pdf");
		writer.setAutogenerateTOCEntries(true);
		doc.open();
		insertTitle(doc,"专利分析报告");
		doc.setMargins(50, 50, 200,0);
		String FILE_DIR_IMG = Coordinator.fileBasePath+taskId+Coordinator.imgPath;
		File file = new File(FILE_DIR_IMG);
		File [] files = file.listFiles();
		for (int i = 0; i < files.length; i++)
        {
            File file1 = files[i];
            file1.getName();   //根据后缀判断
            Image img = Image.getInstance(file1.getPath());
            String des = "专利报告是国家知识产权局根据专利权人或者利害关系人的请求，对相关实用新型专利或者外观设计专利进行检索，并就该专利是否符合专利法及其实施细则规定的授权条件进行分析和评价，作出的报告。专利报告是人民法院或者管理专利工作的部门审理、处理专利侵权纠纷的证据.";
            insertPair(doc,des,img);
        }
		doc.close();
		writer.close();
	}
	public static void makeReport(Long userId,Long depotId) throws DocumentException, IOException {
		String FILE_DIR_PDF = Coordinator.fileBasePath+userId+"\\"+depotId+"\\"+Coordinator.wordPath;
		Document doc = new Document();  
		//doc.topMargin();
		
		File realPathDirectory = new File(FILE_DIR_PDF);
	    if (realPathDirectory == null || !realPathDirectory.exists()) {
	      realPathDirectory.mkdirs();
	    }
	    System.out.println(FILE_DIR_PDF);
		doc.setMargins(50, 50, 350,0);  //left//right//up//down
		RtfWriter2 writer = RtfWriter2.getInstance(doc, new FileOutputStream(FILE_DIR_PDF + "/result.doc")); 
		File uploadFile = new File(realPathDirectory.getPath() + "/" + "result.doc");
		writer.setAutogenerateTOCEntries(true);
		doc.open();
		insertTitle(doc,"专利分析报告");
		doc.setMargins(50, 50, 200,0);
		String FILE_DIR_IMG = Coordinator.fileBasePath+userId+"\\"+depotId+"\\"+Coordinator.imgPath;
		File file = new File(FILE_DIR_IMG);
		File [] files = file.listFiles();
		for (int i = 0; i < files.length; i++)
        {
            File file1 = files[i];
            file1.getName();   //根据后缀判断
            Image img = Image.getInstance(file1.getPath());
            String des = "专利报告是国家知识产权局根据专利权人或者利害关系人的请求，对相关实用新型专利或者外观设计专利进行检索，并就该专利是否符合专利法及其实施细则规定的授权条件进行分析和评价，作出的报告。专利报告是人民法院或者管理专利工作的部门审理、处理专利侵权纠纷的证据.";
            insertPair(doc,des,img);
        }
		doc.close();
		writer.close();
	}
	public static void insertPair(Document document,String des,Image img) throws DocumentException, IOException {
		document.newPage();
		insertGraph(document,img);
		document.add(Chunk.NEWLINE);
		//insertDes(document,des);
	}
	public static void insertDes(Document document,String s) throws DocumentException, IOException {

		File file=new File(ReportWord.class.getClassLoader().getResource("fzktjw.ttf").getFile());
		kaiti = BaseFont.createFont("fzktjw.ttf",
				BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font TitleFont = new Font(kaiti, 15, Font.NORMAL); 
		Paragraph title = new Paragraph(s, TitleFont);
		title.setAlignment(Element.ALIGN_LEFT);
		document.add(title);   
	}
	public static void insertGraph(Document document,Image img) throws DocumentException {
		img.setAlignment(Image.MIDDLE | Image.TEXTWRAP);  
		img.setBorder(Image.BOX);  
		img.setBorderWidth(10);  
		
		//img.setBorderColor(BaseColor.WHITE);  
		img.scaleToFit(1000, 250);
		document.add(img);  
	}
	public static void insertTitle(Document document,String s) throws DocumentException, IOException {
		//document.newPage();
		File file=new File(ReportWord.class.getClassLoader().getResource("simsun.ttc").getFile());
		chinese = BaseFont.createFont(file.getPath()+",1",
				BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font titleFont = new Font(chinese, 50, Font.NORMAL); 
		Paragraph title = new Paragraph(s, titleFont);
		title.setAlignment(Element.ALIGN_CENTER);
		
		document.add(title);
		
		document.add(Chunk.NEWLINE);  
		Font authorFont = new Font(chinese, 20, Font.NORMAL); 
		//Paragraph author = new Paragraph("-----游弋", authorFont);
		//author.setAlignment(Element.ALIGN_RIGHT);
		//document.add(author);
	}
}

